/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.financing.service;

import com.fzhucc.modules.financing.domain.Financing;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.financing.repository.FinancingRepository;
import com.fzhucc.modules.financing.domain.dto.FinancingDto;
import com.fzhucc.modules.financing.domain.dto.FinancingQueryCriteria;
import com.fzhucc.modules.financing.service.mapstruct.FinancingMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2023-06-01
**/
@Service
@RequiredArgsConstructor
public class FinancingService {

    private final FinancingRepository financingRepository;
    private final FinancingMapper financingMapper;

    public Map<String,Object> queryAll(FinancingQueryCriteria criteria, Pageable pageable){
        Page<Financing> page = financingRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(financingMapper::toDto));
    }

    public List<FinancingDto> queryAll(FinancingQueryCriteria criteria){
        return financingMapper.toDto(financingRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Transactional
    public FinancingDto findById(Integer id) {
        Financing financing = financingRepository.findById(id).orElseGet(Financing::new);
        ValidationUtil.isNull(financing.getId(),"Financing","id",id);
        return financingMapper.toDto(financing);
    }

    @Transactional(rollbackFor = Exception.class)
    public FinancingDto create(Financing resources) {
        return financingMapper.toDto(financingRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(Financing resources) {
        Financing financing = financingRepository.findById(resources.getId()).orElseGet(Financing::new);
        ValidationUtil.isNull( financing.getId(),"Financing","id",resources.getId());
        financing.copy(resources);
        financingRepository.save(financing);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            financingRepository.deleteById(id);
        }
    }

    public void download(List<FinancingDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (FinancingDto financing : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("项目名称", financing.getName());
            map.put("所属单位", financing.getUnit());
            map.put("融资需求(万元)", financing.getNeeds());
            map.put("融资金额（万元）", financing.getAmount());
            map.put("类型 融资企业 融资需求", financing.getType());
            map.put("是否显示", financing.getIsShow());
            map.put("排序", financing.getSort());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}